package com.supcalls.controller;
import com.supcalls.service.RedisLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisLockController {

    @Autowired
    private RedisLockService redisLockService;

    @GetMapping("/testLock")
    public String testLock() {
        String lockKey = "lock:test";

        boolean isLocked = redisLockService.tryLock(lockKey);
        if (isLocked) {
            try {
                // 模拟业务逻辑处理
                Thread.sleep(5000);
                return "Lock acquired and business logic executed successfully!";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "An error occurred!";
            } finally {
                redisLockService.unlock(lockKey);
            }
        } else {
            return "Failed to acquire lock!";
        }
    }
}
